/* eslint-disable dot-notation */
class MainFocus {
	Main: egret.DisplayObject;

	constructor(Main: egret.DisplayObject) {
		this.Main = Main;
		window.document.addEventListener('keydown', (ev: KeyboardEvent) =>
			this.onWindowKey(ev, 'down'),
		);
		Main.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTapBegin, this);
		Main.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
	}

	onTapBegin(evt: egret.TouchEvent): void {
		if (isDebug()) {
			// console.debug('MainFocus.onTapBegin:', evt.target);
		}
		this.setFocusTarget(evt.target);
	}

	onTap(evt: egret.TouchEvent): void {
		if (isDebug()) {
			// console.debug('MainFocus.onTap:', evt.target);
		}
		this.setFocusTarget(evt.target);
	}

	clearFocus(why: string): void {
		// console.log('clearFocus', why, this.focus);
		this.focus = null;
	}

	focusStack: egret.DisplayObject[] = [];

	pushFocus(newfocus: egret.DisplayObject): number {
		this.focusStack.push(this.focus);
		// this.clearFocus('push');
		// ui.log('pushFocus', this.focus && this.focus.constructor.name, '=>', newfocus.constructor.name);
		this.focus = newfocus;
		return this.focusStack.length;
	}

	popFocus(focus: egret.DisplayObject): void {
		// if (this.focus === focus) {
		this.focus = this.focusStack.pop();
		// }
		// ui.log('popFocus', this.focus && this.focus.constructor.name);
	}

	focus: egret.DisplayObject;

	setFocusTarget(obj: egret.DisplayObject): void {
		this.focus = obj;
	}

	getKeyHandle(obj: egret.DisplayObject): egret.DisplayObject {
		while (obj) {
			if (obj['onKey'] || obj instanceof eui.EditableText) {
				return obj;
			}
			obj = obj.$parent;
		}
		return null;
	}

	static inputEnterEvent = 'ENV_INPUTENTER';

	static inputTabEvent = 'ENV_INPUTTAB';

	onWindowKey(ev: KeyboardEvent, status: string): void {
		// console.debug('MainFocuse.onWindowKey', ev.key, status);
		// console.debug(ev, this.focus);
		if (isDebug()) {
			console.log('MainFocuse.onWindowKey', ev.key, this.focus);
		}

		if (ev.key === 'Escape') {
			// this.clearFocus('Escape');
			return;
		}

		const obj = this.focus;
		if (!obj) {
			return;
		}

		if (!isVisible(obj)) {
			const b = isVisible(obj);
			this.clearFocus('!isVisible');
			return;
		}

		if (isDebug()) {
			console.log('MainFocuse.onWindowKey', ev.key, this.focus);
		}

		const objkey = this.getKeyHandle(obj);
		if (!objkey) {
			// if (game.getMapMainChar()) {
			// 	// console.log('!objkey', obj);
			// 	if (obj === game.getMapMainChar() || obj === game.GameMap.ins()) {
			// 		if (!ev.ctrlKey && !ev.altKey) {
			// 			game.getMapMainChar().onMainKey(ev);
			// 		}
			// 	}
			// }
			return;
		}

		if (objkey instanceof eui.EditableText) {
			if (status === 'down') {
				if (ev.key === 'Enter') {
					objkey.dispatchEventWith(MainFocus.inputEnterEvent, true, ev);
					return;
				}
				if (ev.key === 'Tab') {
					// tab会把焦点切换到浏览器
					objkey.dispatchEventWith(MainFocus.inputTabEvent, true, ev.shiftKey);
				}
			}
		} else if (objkey['onKey']) {
			const rt = objkey['onKey'](ev, status, obj);
		}
	}
}
